package com.sz.ac.services.ac;

import com.sz.ac.entity.AcBallLog;
import com.sz.ac.entity.AcGrp;
import com.sz.ac.entity.AcInvest;
import com.sz.ac.entity.AcInvestLog;
import com.sz.ac.interfs.IAcInvestService;
import com.sz.ac.model.constant.AddBallEnum;
import com.sz.ac.model.constant.Vars;
import com.sz.ac.services.mapper.ac.AcGrpMapper;
import com.sz.ac.services.mapper.ac.AcInvestMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户投资表 服务实现类
 * </p>
 *
 * @author ruichar
 * @since 2018-03-24
 */
@Service
public class AcInvestServiceImpl extends ServiceImpl<AcInvestMapper, AcInvest> implements IAcInvestService 
{
	private static final Logger logger = LoggerFactory.getLogger(AcInvestServiceImpl.class);
	@Resource
	private AcInvestMapper investMapper;
	@Resource
	private AcGrpMapper acGrpMapper;

	@Transactional
	public void importInvestData(List<AcInvest> list) 
	{
		Random random = new Random();
		Date now = new Date();
		//数据库中有没有记录校验
		for(AcInvest item :list)
		{
			String phone = item.getPhone();
			AcInvest checkParam = new AcInvest();
			checkParam.setPhone(phone);
			AcInvest tableAc = investMapper.selectOne(checkParam);
			if(tableAc!=null && tableAc.getId()!=null && tableAc.getId()>0 )
			{
				boolean isHasGrp = tableAc.getGrpId()!=null && tableAc.getGrpId()>0;
				//有队伍的投资才进行加球
				if(isHasGrp)
				{
					int ball = 0;
					//首投
					if(tableAc.getAmount().compareTo(BigDecimal.ZERO)<=0)
					{
						logger.info("======phone={},first invest,amount={}",item.getPhone(),item.getAmount());
						ball = getFirstInvestBall(item.getAmount());
		
					}
					else
					{
						ball = getMultiInvestBall(item.getAmount());
					}
					tableAc.setInvestBall(tableAc.getInvestBall()+ball);
					
					AcBallLog acBallLog = new AcBallLog();
					acBallLog.setAmount(item.getAmount());
					acBallLog.setBall(ball);
					acBallLog.setDesc(AddBallEnum.inveset_add.name());
					acBallLog.setGrpId(tableAc.getGrpId());
					acBallLog.setPhone(item.getPhone());
					acBallLog.setTsp(now);
					acBallLog.insert();
					//战队+
					AcGrp grp = acGrpMapper.selectById(tableAc.getGrpId());
					grp.setBallNm(grp.getBallNm()+ball);
					grp.setTsp(now);
					grp.updateById();
				}
				tableAc.setAmount(tableAc.getAmount().add(item.getAmount()));
				tableAc.setInvestTime(item.getInvestTime());
				tableAc.updateById();
			}
			else
			{
				item.setPicIndex(random.nextInt(9)+1);
				item.insert();
			}
			
			AcInvestLog importLog = new AcInvestLog();
			importLog.setAmount(item.getAmount());
			importLog.setPhone(item.getPhone());
			importLog.setInvestTime(item.getInvestTime());
			importLog.setTsp(now);
			importLog.insert();
			
		}
	}

	public List<AcInvest> getAllInvest() 
	{
		return investMapper.getAllInvest();
	}
	
	//进球：组员投资可以贡献进球，1000=<首投金额<5000,1个球;
	//5000<首投金额<=10000,2个球;10000<=首投金额,3个球;
	//5000<=复投,1个球3.2、次月1日更新上月战队总球数排名，
	
	/**
	 * @param amount
	 * @return
	 * 首投
	 */
	private int getFirstInvestBall(BigDecimal amount)
	{
		int ball = 0;
		if(amount.compareTo(new BigDecimal(1000))>=0 && amount.compareTo(new BigDecimal(5000))<0){
			ball = 1;
		}
		else if(amount.compareTo(new BigDecimal(5000))>=0 && amount.compareTo(new BigDecimal(10000))<0)
		{
			ball = 2;
		}else if(amount.compareTo(new BigDecimal(10000))>=0 ){
			ball = 3;
		}
		return ball;
	}
	
	/**
	 * @param amount
	 * @return
	 * 复投
	 */
	private int getMultiInvestBall(BigDecimal amount)
	{
		int ball = 0;
		if(amount.compareTo(new BigDecimal(5000))>=0){
			ball = 1;
		}
		return ball;
	}
}
